import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('../components/login')
const Home = () => import('../components/Home')
const Welcome = () => import('../components/Welcome')
const User = () => import('../components/User')
const Rights = () => import('../components/Rights')
const Roles = () => import('../components/Roles')
const Categories = () => import('../components/Categories')
const Params = () => import('../components/Params')
const List = () => import('../components/List')
const Addgood = () => import('../components/Addgood')
const Order = () => import('../components/Order')
const Report = () => import('../components/Report')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/good',
        component: Addgood
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }

]

const router = new VueRouter({
  routes
})
// 路由导航守卫判断访问权限的
router.beforeEach((to, from, next) => {
  // 判断路径
  if (to.path === '/login') {
    return next()
  }
  const token1 = window.sessionStorage.getItem('token')
  if (token1) {
    return next()
  } else {
    return next('/login')
  }
})

export default router
